package com.wm.blog_search_interface.feign;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_common.constatnt.CommonConstant;
import com.wm.blog_common.result.Result;
import com.wm.blog_config.Document.BlogDocument;
import com.wm.blog_config.config.CustomFeignConfig;
import com.wm.blog_search_interface.feign.factory.SearchClientFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/***
 * @ClassName: PictureFeignClient
 * @Description: search feign调用  todo feign使用get有坑啊,是否考虑使用HttpClient替换feign的HttpURLConnection，采用apache的HttpClient
 * @Author: 半卷流年
 * @Create_time: 2020-5-23
 */
@FeignClient(value = CommonConstant.SEARCH_MODULE_NAME, configuration = CustomFeignConfig.class, fallbackFactory = SearchClientFallbackFactory.class)
public interface SearchFeignClient {

    /**
     * 新增
     * @return
     */
    @PostMapping("/blog/es/save")
     Result add(@RequestBody BlogDocument blogDocument);

    /**
     * 删除索引
     * @return Result
     */
    @DeleteMapping("/blog/es/delete/{id}")
     Result delete(@PathVariable("id") Long id);
    /**
     * 清空索引
     * @return
     */
    @DeleteMapping("/blog/es/deleteAll")
     Result deleteAll();

    /**
     * 根据ID获取
     * @return
     */
    @GetMapping("/blog/es/get/{id}")
     Result<BlogDocument> getById(@PathVariable("id") Long id);

    /**
     * 根据获取全部
     * @return
     */
    @GetMapping("/blog/es/getAll")
     Result<List<BlogDocument>> getAll();

    /**
     * 搜索
     * @param keyword
     * @return
     */
    @GetMapping("/blog/es/query/{keyword}")
     Result<List<BlogDocument>> query(@PathVariable("keyword") String keyword);

    /**
     * 搜索，命中关键字高亮
     * @param keyword   关键字
     * @param indexName 索引库名称
     * @param fields    搜索字段名称，多个以“，”分割
     * @return
     */
    @GetMapping("/blog/es/queryHit")
     Result<List<Map<String,Object>>> queryHit(@RequestParam("keyword") String keyword, @RequestParam String indexName, @RequestParam String fields);

    /**
     * 搜索，命中关键字高亮
     * http://localhost:8080/query_hit_page?keyword=无印良品荣耀&indexName=orders&fields=productName,productDesc&pageNo=1&pageSize=1
     * @param keyword   关键字
     * @param indexName 索引库名称
     * @param fields    搜索字段名称，多个以“，”分割
     * @return
     */
    @GetMapping("/blog/es/queryHitPage")
     Result<Page<Map<String,Object>>> queryHitByPage(@RequestParam int current, @RequestParam int size
            , @RequestParam String keyword, @RequestParam String indexName, @RequestParam String fields);

    /**
     * 删除索引库
     * @param indexName
     * @return
     */
    @DeleteMapping("/blog/es/deleteIndex/{indexName}")
     Result<String> deleteIndex(@PathVariable("indexName") String indexName);


    /**
     * 修改文档数据,局部数据更新,必须保证id存在
     * @param document
     * @return
     */
    @PutMapping("/blog/es/updateDocument")
     Result updateDocument(@RequestBody BlogDocument document);

}
